

package com.hazelcast.test.starter.constructor;

import java.lang.reflect.Method;

/**
 * Constructs enum instances using its {@link Enum#name()} and
 * {@link Enum#valueOf(Class, String)} methods.
 */
public class EnumConstructor extends AbstractStarterObjectConstructor {

    public EnumConstructor(Class<?> targetClass) {
        super(targetClass);
    }

    @Override
    Object createNew0(Object delegate) throws Exception {
        // obtain delegate as string
        Method nameMethod = delegate.getClass().getMethod("name");
        String delegateAsString = (String) nameMethod.invoke(delegate);
        // construct new instance at target classloader via valueOf()
        Method valueOf = targetClass.getMethod("valueOf", String.class);

        return valueOf.invoke(null, delegateAsString);
    }
}
